//
//  main.swift
//  Swift面向对象
//
//  Created by durban.zhang on 2018/5/9.
//  Copyright © 2018年 durban.zhang. All rights reserved.
//

import Foundation

print("8、Swift - 类和结构体")

// 类和结构体的定义

// 类定义语法
class SomeClass {
    
}

// 结构体定义语法
struct SomeStruct {
    
}

/*
 
 当你定义一个新的类和结构体时，你定义了一个有效的的Swift类型。
 给类型命名时使用UpperCamelCase的方式（如SomeClass和SomeStructure这里）以匹配标准的Swift类型（如String，Int和Bool）的大小写。 相反，使用lowerCamelCase的名称方式（如frameRate和incrementCount）去命名属性和方法。
 */

struct Resolution {
    var width = 0
    var height = 0
}

class VideoMode {
    var resolution = Resolution()
    var frameRate = 0.0
    var name: String?
}

// 类和结构体的实例化

let someResolution = Resolution()
let someVideoMode = VideoMode()

// 属性的调用
print("The width of someResolution is \(someResolution.width)")
print("The width of somsVideoMode is \(someVideoMode.resolution.width)")

// 给属性赋值新的值
someVideoMode.resolution.width = 100
print("The width of somsVideoMode is \(someVideoMode.resolution.width)")

// 结构体类型的初始化
let vga = Resolution(width: 640, height: 480)
print("The width is \(vga.width) and the height is \(vga.height)")

/* 结构体和枚举都是值类型（value type）
 当结构体和枚举赋值给常量或变量的时候，结构体和枚举会被复制然后在赋值
 */
let hd = Resolution(width: 1920, height: 1080)
var cinema = hd
print("The cinema width is \(cinema.width)")
print("The hd width is \(hd.width)")
cinema.width = 2048
print("The new cinema width is \(cinema.width)")
print("The hd width is \(hd.width)")

// 枚举
enum CompassPoint {
    case north, south, east, west
}

var currentDirection = CompassPoint.west
let rememberDirection = currentDirection

currentDirection = .east

if rememberDirection == .west {
    print("rememberDirection still is \(rememberDirection)")
}
/*
 当我们给rememberDirection赋值为currentDirection的值后，再去改变currentDirection的值，rememberDirection的值没有变化，存储的还是原始的值
 */

/*
 类是引用类型
 不像值类型，引用类型在赋值给变量的时候，值不会被复制
 */
let tenEighty = VideoMode()
tenEighty.resolution = hd
tenEighty.frameRate = 25.0
tenEighty.name = "1080i"
print(tenEighty)
print("tenEighty frameRate is \(tenEighty.frameRate)")
print("tenEighty name is \(tenEighty.name!)")
print("tenEighty resolution width is \(tenEighty.resolution.width)")
print("tenEighty resolution height is \(tenEighty.resolution.height)")

// 下面将tenEighty赋值给一个新的常量
let alsoTenEighty = tenEighty
alsoTenEighty.frameRate = 30
print("=======================")
print("tenEighty frameRate is \(tenEighty.frameRate)")
print("tenEighty name is \(tenEighty.name!)")
print("tenEighty resolution width is \(tenEighty.resolution.width)")
print("tenEighty resolution height is \(tenEighty.resolution.height)")
print("=======================")
print("alsoTenEighty frameRate is \(alsoTenEighty.frameRate)")
print("alsoTenEighty name is \(alsoTenEighty.name!)")
print("alsoTenEighty resolution width is \(alsoTenEighty.resolution.width)")
print("alsoTenEighty resolution height is \(alsoTenEighty.resolution.height)")

// 由于类是引用类型，tenEighty和alsoTenEighty都是VideoMode的实例，所以alsoTenEighty的修改会直接修改VideoMode的实例

/*
 恒等运算
 
 因为类是引用类型，可能一个类会赋值给多个变量或常量
 有时候对于找出两个变量或常量之间是不是同一个类是很有必要的，swift提供了两个操作符
 
 恒等于 (===)
 不恒等于 (!==)
 
 使用这些运算符来检查两个变量或常量是不是属于同一个类，如下
 */
if (tenEighty === alsoTenEighty) {
    print("tenEighty and alsoTenEighty refer to the same VideoMode instance")
}

/*
 根据上面的简单介绍，实际应用中我们要如何选择使用类还是结构体，应该是有所掌握的了
 
 */
 























